#!/bin/bash
# 腾讯云部署脚本

# 1. 安装基础依赖
sudo apt update
sudo apt install -y git nodejs npm nginx mysql-server

# 2. 配置MySQL数据库
sudo mysql -e "CREATE DATABASE IF NOT EXISTS vx_msg_push CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -e "CREATE USER 'vxadmin'@'localhost' IDENTIFIED BY 'your_secure_password';"
sudo mysql -e "GRANT ALL PRIVILEGES ON vx_msg_push.* TO 'vxadmin'@'localhost';"

# 3. 克隆项目代码
git clone https://your-git-repo.com/VxMsgPush.git /var/www/vxmsgpush
cd /var/www/vxmsgpush
npm install --production

# 4. 配置环境变量
cat > .env <<EOF
NODE_ENV=production
ADMIN_USERNAME=your_prod_admin
ADMIN_PASSWORD=your_prod_password
DB_HOST=localhost
DB_NAME=vx_msg_push
DB_USER=vxadmin
DB_PASS=your_secure_password
WECHAT_APPID=your_appid
WECHAT_APPSECRET=your_appsecret
EOF

# 5. 配置Nginx反向代理
cat > /etc/nginx/sites-available/vxmsgpush <<EOF
server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host \$host;
        proxy_cache_bypass \$http_upgrade;
    }
}
EOF

# 6. 启用配置并重启Nginx
ln -s /etc/nginx/sites-available/vxmsgpush /etc/nginx/sites-enabled
nginx -t && systemctl restart nginx

# 7. 配置PM2进程管理
npm install -g pm2
pm2 start app.js --name "vxmsgpush"
pm2 save
pm2 startup